<?php
App::uses('AppController', 'Controller');
/**
 * Previews Controller
 *
 * @property Preview $Preview
 */
class PreviewsController extends AppController {

        public $uses =array('Preview','Upload');
	//public $components = array('RequestHandler');    
	public $flag=0;
	//public $helpers = array('Ajax','Js');
	//var $components = array('RequestHandler');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Preview->recursive = 0;
		$this->set('previews', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Preview->exists($id)) {
			throw new NotFoundException(__('Invalid preview'));
		}
		$options = array('conditions' => array('Preview.' . $this->Preview->primaryKey => $id));
		$this->set('preview', $this->Preview->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		
		if ($this->request->is('post')) {
			$this->Preview->create();
			$this->flag=1;
			$this->Upload->create();
			if ($this->uploadFile() && $this->Upload->save($this->data['Upload'])) 
			{	
				$this->request->data['Preview']['presentation_id'] = Cache::read('presentation_id');
				$user = $this->Session->read('Auth.User');
				$this->request->data['Preview']['user_id']=$user['id'];
				if ($this->Preview->save($this->data['Preview'])) {
					$this->Session->setFlash(__('El avance fue guardado con éxito.'));
					Cache::delete('presentation_id');
					$anterior_page=Cache::read('anterior_page');
					Cache::delete('anterior_page');
					//$this->redirect(array('action' => 'index'));
					$this->redirect($anterior_page);
				} else {
					$this->Session->setFlash(__('El avance no se pudo guardar. Por favor,intenta otra vez.'));
				}
			}
		}
		$presentations = $this->Preview->Presentation->find('list');
		$uploads = $this->Preview->Upload->find('list');
		$this->set(compact('presentations', 'uploads'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Preview->exists($id)) {
			throw new NotFoundException(__('Invalid preview'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Preview->save($this->request->data)) {
				if($this->editUploadFile($this->request->data['Preview']['upload_id']))	
				   $this->Upload->save($this->request->data['Upload']);					
				$this->Session->setFlash(__('The preview has been saved'));
				$this->redirect(array('action' => 'index'));
				
			} else {
				$this->Session->setFlash(__('The preview could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Preview.' . $this->Preview->primaryKey => $id));
			$this->request->data = $this->Preview->find('first', $options);
		}
		$presentations = $this->Preview->Presentation->find('list');
		$uploads = $this->Preview->Upload->find('list');
		$this->set(compact('presentations', 'uploads'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$params = explode('|',$id);
		$this->Preview->id = $params[0];
		if (!$this->Preview->exists()) {
			throw new NotFoundException(__('Avance invalido.'));
		}
		$this->request->onlyAllow('post', 'delete');
		$this->Upload->id = $params[1];
		
		if ($this->Preview->delete() && $this->Upload->delete()) {
			//print_r();
			$this->Session->setFlash(__('Avance eliminado.'));
			$file = new File(APP.'uploads'.DS.$params[1],false,0777);
			if($file->delete()) {
				$this->redirect($this->referer());
				//$this->redirect('/fronts/indexAlumno');
   				//echo 'image deleted.....';
			}
			//$this->redirect(array('action' => 'index'));
			$this->redirect($this->referer());			
			//$this->redirect('/fronts/indexAlumno');
			//print_r("nada");
		}
		$this->Session->setFlash(__('El avance no fue eliminado.'));
		//$this->redirect(array('action' => 'index'));
	}

	function uploadFile() {
		  		
		$file = $this->data['Upload']['file'];
  		if ($file['error'] === UPLOAD_ERR_OK) {
    			$id = String::uuid();
    			if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {
      				//$data1 = array("Upload" => array("id" => "0","user_id" => "0","filename" => "0","filesize" => "0","filemime" => "0"));
      				$this->request->data['Upload']['id'] = $id;
      				$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
      				$this->request->data['Upload']['filename'] = $file['name'];
      				$this->request->data['Upload']['filesize'] = $file['size'];
      				$this->request->data['Upload']['filemime'] = $file['type'];
				$this->request->data['Preview']['upload_id']=$id;
      				return true;
    			}
  		}
  		return false;
	}
	
	function editUploadFile($id) {
  		$file = $this->request->data['Upload']['file'];
  		if ($file['error'] === UPLOAD_ERR_OK) {
    			//$id = String::uuid();
    			if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {
      				$this->request->data['Upload']['id'] = $id;
      				$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
      				$this->request->data['Upload']['filename'] = $file['name'];
      				$this->request->data['Upload']['filesize'] = $file['size'];
      				$this->request->data['Upload']['filemime'] = $file['type'];
      				return true;
    			}
  		}
  		return false;
	}

	/*function termino()
	{
		$image ="<?php echo $this->html->image('cargando.gif');?>";		
		$this->autoRender = false;
		if ( $this->RequestHandler->isAjax() ) {
          		Configure::write ( 'debug', 0);
     		}
		
		if($this->flag==1){
		 
		   echo "<img src='/taller1-dtc/dtcapp2/img/cargando.gif' alt=''>";
		   //$this->set('actualizacion',$this->html->image('cargando.gif'));
		   $this->render('termino','ajax');
		}
		else{
			echo $this->flag;//<img src='/taller1-dtc/dtcapp2/img/cargando.gif' alt=''>";
			$this->render('termino','ajax');
		}
	}*/
	function redirect1()
	{
		$this->redirect('/users/index');
	}
}
